//JSONData[page][row] = {id,time,user,realname,email,pho};


$(function () {

    // nav click
    // $("#mynav li").click(function (e) {
    //     e.preventDefault();
    //     $(this).tab("show");
    // });
// 当前也与总页数
    var curPage = 1;
    var pageSum = 10;

    // 页面加载方法
    var loadPage = function (curPage) {
        //jsonData在正式交互中使用ajax获得
        // var htmlcode = '<table class="table datatable table-striped "><thead><tr><th data-type="number" width="80px">#</th><th data-type="date" width="160px">时间</th><th>用户名</th><th>真实姓名</th><th>邮箱</th><th>电话</th><th class="sort-disabled" width="80px">操作</th></tr></thead><tbody>';
        // for (var i = 0; i < JSONData[curPage - 1].length; ++i) {
        //     htmlcode += '<tr><td>' + JSONData[curPage - 1][i].id + '</td><td>' + JSONData[curPage - 1][i].time + '</td><td>' + JSONData[curPage - 1][i].user + '</td><td>' + JSONData[curPage - 1][i].realname + '</td> <td>' + JSONData[curPage - 1][i].email + '</td><td>' + JSONData[curPage - 1][i].pho + '</td><td><a href="#"><i class="icon icon-edit" data-toggle="modal" data-target="#editModal"></i></a>&nbsp;&nbsp;<a href="#" ><i class="icon icon-trash" data-toggle="modal" data-target="#delModal"></i></a></td></tr>';
        // }
        // htmlcode += '</tbody></table>';
        //
        // // 将#mycontent的html代码更新，并且初始化表格
        // $('#mycontent').html(htmlcode);
        // $('table.datatable').datatable({
        //     checkable: true,
        //     sortable: true,
        //     checkedClass: "active",
        // });

        var now = new Date();
        data = {
            // cols: [
            //     {field: 'id', width: 80, text: '#', type: 'number', flex: false, colClass: 'text-center'},
            //     {field: 'time', width: 160, text: '时间', type: 'date', flex: false, sort: 'down'},
            //     {field: 'name', width: 80, text: '名称', type: 'string', flex: true, colClass: ''},
            //     // {field: 'operation', sort: false, width: 'auto', text: '操作', type: 'string', flex: false, colClass: ''},
            // ],

            cols: [
                {width: 80, text: '#', type: 'number', flex: false},
                {width: 160, text: '时间', type: 'date', flex: false, sort: 'down'},
                {width: 80, text: '名称', type: 'string', flex: true},
                {field: 'operation', sort: false, width: 'auto', text: '操作', type: 'string', flex: false, colClass: ''}
            ],
            // 行数据
            rows: []
        };

        for (var i = 1; i < 10; i++) {
            var row = {
                checked: false,
                data: [
                    i,
                    now.format('yyyy-MM-dd hh:mm:ss'),
                     "jj"
               ]
            }

            data.rows.push(row);
            now = new Date(now.getTime() - (Math.random() * 1000 * 60 * 60));
        }
        ;

        console.log(data);
        $('table.datatable').datatable({
            data: data,
            checkable: true,
            sortable: true,
            checkedClass: "active",
            afterLoad: function (e) {
                $.each(e.data.rows, function (index, row) {
                    row.data[3]= "<a href='#'><i class='icon icon-edit' data-toggle='modal' data-target='#editModal'></i></a>&nbsp;&nbsp;<a href='#' ><i class='icon icon-trash' data-toggle='modal' data-target='#delModal'></i></a>"
                })
            }
        });

        $('.pager .currentPage input').val(curPage);
        $('.pager .currentPage label').text(pageSum);
    }
    //加载第一页
    loadPage(1);
    //分页查询,,
    $('.pager li:not(.disabled) a i').click(function () {
        if ($(this).hasClass('previous')) {     // 当前点击的是上一页
            curPage = --curPage <= 1 ? 1 : curPage; // 更新页码
        }
        else if ($(this).hasClass('next')) {    // 当前点击的是下一页
            curPage = ++curPage >= pageSum ? pageSum : curPage;
        }else if($(this).hasClass('first')){
            curPage  =1;
        }else if($(this).hasClass('last')){
            curPage = pageSum;
        }

        // 判断如果当前是第一页，给previous加上disabled
        if (curPage == 1) {
            $(this).closest('.pager').find('.previous').addClass('disabled');
        }
        else {
            $(this).closest('.pager').find('.previous').removeClass('disabled');
        }

        // 判断如果当前是最后一页，给next加上disabled
        if (curPage == pageSum) {
            $(this).closest('.pager').find('.next').addClass('disabled');
        }
        else {
            $(this).closest('.pager').find('.next').removeClass('disabled');
        }
        // htmlcode 为更新的表格
        loadPage(curPage);
    });

    //修改对话框打开时加载单行数据
    $('#editModal').on('shown.zui.modal', function () {
        $("#exampleInputAccountEdit").val("hhhh");
        $("#exampleInputPasswordEdit").val("hhhh");
        $("#exampleInputAddress1Edit").val("hhhh");
    })


    //修改记录
    $('#editModalSure').click(
        function () {
            if(!Tool.validForm($("#editForm"))){
                return;
            }
            var user = {};
            user.exampleInputAccountAdd = $("#exampleInputAccountAdd").val();
            user.exampleInputPasswordAdd = $("#exampleInputPasswordAdd").val();
            user.exampleInputAddress1Add = $("#exampleInputAddress1Add").val();

            $.ajax({
                type: "post",
                async: false,
                url: "/youcai/user/edit",
                data: user,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (true) {
                        loadPage(1);
                        $("#successResult").modal({
                            keyboard: false,
                            show: true
                        })
                    } else {
                        $("#failResult").modal({
                            keyboard: false,
                            show: true
                        })
                    }
                },
                error: function (error) {
                    console.log('send fail');
                }
            });
            $('#editModal').modal('hide')
        }
    )

    //增加记录
    $('#addModalSure').click(
        function (e) {
            if(!Tool.validForm($("#addForm"))){
                 return;
            }
            var user = {};
            user.exampleInputAccountAdd = $("#exampleInputAccountAdd").val();
            user.exampleInputPasswordAdd = $("#exampleInputPasswordAdd").val();
            user.exampleInputAddress1Add = $("#exampleInputAddress1Add").val();
            $.ajax({
                type: "post",
                async: false,
                url: "/youcai/user/add",
                data: user,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (true) {
                        loadPage(1);
                        $("#successResult").modal({
                            keyboard: false,
                            show: true
                        })
                    } else {
                        $("#failResult").modal({
                            keyboard: false,
                            show: true
                        })
                    }
                },
                error: function (error) {
                    console.log('send fail');
                }
            });
            $('#addModal').modal('hide')
        }
    )


    //删除记录
    $('#delModalSure').click(
        function () {
            var user = {};
            user.exampleInputAccountAdd = "test";
            $.ajax({
                type: "post",
                async: false,
                url: "/youcai/user/del",
                data: user,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (true) {
                        loadPage(1);
                        $("#successResult").modal({
                            keyboard: false,
                            show: true
                        })
                    } else {
                        $("#failResult").modal({
                            keyboard: false,
                            show: true
                        })
                    }
                },
                error: function (error) {
                    console.log('send fail');
                }
            });
        }
    )


});